package com.oig.sys.file.dao;

import com.oig.sys.file.entity.TbFileInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbFileInfoDao extends JpaRepository<TbFileInfo, Integer>, JpaSpecificationExecutor<TbFileInfo> {

    @Query("from TbFileInfo f where f.id=:id")
    TbFileInfo getByFileInfoId(@Param("id") Integer id);

    @Query("select f from TbFileInfo f where f.businessType=:businessType and (:businessOrderId is null or f.businessOrderId=:businessOrderId) " +
            " and f.relationType=:relationType and (:relationId is null or f.relationId=:relationId) and f.id not in(:idList)")
    List<TbFileInfo> findByRelationIdExIds(@Param("businessType")String businessType, @Param("businessOrderId")String businessOrderId,
                                           @Param("relationType") String relationType,@Param("relationId") String relationId,
                                           @Param("idList") List<Integer> idList);

    @Query("select f from TbFileInfo f where f.businessType=:businessType and (:businessOrderId is null or f.businessOrderId=:businessOrderId) " +
            " and f.relationType=:relationType and (:relationId is null or f.relationId=:relationId)")
    List<TbFileInfo> findByRelationId(@Param("businessType")String businessType, @Param("businessOrderId")String businessOrderId,
                                      @Param("relationType")String relationType, @Param("relationId")String relationId);


    @Query("select count(f) from TbFileInfo f where f.businessType=:businessType and f.businessOrderId=:businessOrderId" +
            " and f.formalType=:formalType")
    Integer countFile(@Param("businessType")String businessType,
                      @Param("businessOrderId")String businessOrderId,
                      @Param("formalType")String formalType);

    @Query("select count(f) from TbFileInfo f where f.businessType=:businessType and (:businessOrderId is null or f.businessOrderId=:businessOrderId)" +
            " and f.relationType=:relationType and (:relationId is null or f.relationId=:relationId)" +
            " and f.formalType=:formalType")
    Integer countFile(@Param("businessType")String businessType,
                      @Param("businessOrderId")String businessOrderId,
                      @Param("relationType")String relationType,
                      @Param("relationId")String relationId,
                      @Param("formalType")String formalType);


}
